﻿Imports OutlookStyleControls
Imports System.Collections
Imports System.ComponentModel
Imports Janus

Public Class frmQuanLySoKho
    'dung de luu gia tri loc cua lo hang
    'sau do truyen vao form Loc 
    Public fCallPhieuNX As Form = Nothing

    Private mMaHang As String = ""
    Private mTenHang As String = ""
    Private mLoHang As String = ""
    Private mCCL As String = ""
    Private mSSHSD As String = ""
    Private mHSD As Date = Now.Date
    Private mSSSLTon As String = ""
    Private mSLTon As String = ""

    Private mDMLoaiKho As String = ""
    Private mKho As String = ""
    Dim flagHSD As Boolean = False

#Region "Property"
    Property MaHang() As String
        Get
            Return mMaHang
        End Get
        Set(ByVal value As String)
            mMaHang = value
        End Set
    End Property
    Property TenHang() As String
        Get
            Return mTenHang
        End Get
        Set(ByVal value As String)
            mTenHang = value
        End Set
    End Property
    Property LoHang() As String
        Get
            Return mLoHang
        End Get
        Set(ByVal value As String)
            mLoHang = value
        End Set
    End Property
    Property CCL() As String
        Get
            Return mCCL
        End Get
        Set(ByVal value As String)
            mCCL = value
        End Set
    End Property
    Property SSHSD() As String
        Get
            Return mSSHSD
        End Get
        Set(ByVal value As String)
            mSSHSD = value
        End Set
    End Property
    Property HSD() As Date
        Get
            Return mHSD
        End Get
        Set(ByVal value As Date)
            mHSD = value
        End Set
    End Property
    Property SSSLTon() As String
        Get
            Return mSSSLTon
        End Get
        Set(ByVal value As String)
            mSSSLTon = value
        End Set
    End Property
    Property SLTon() As String
        Get
            Return mSLTon
        End Get
        Set(ByVal value As String)
            mSLTon = value
        End Set
    End Property
    Property DMLoaiKho() As String
        Get
            Return mDMLoaiKho
        End Get
        Set(ByVal value As String)
            mDMLoaiKho = value
        End Set
    End Property
#End Region

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        KhoiTaoDMKho()
    End Sub

    'Public Sub New(ByVal ma_loaikho As String)
    '    ' This call is required by the Windows Form Designer.
    '    InitializeComponent()

    '    ' Add any initialization after the InitializeComponent() call.
    '    mDMLoaiKho = ma_loaikho
    '    KhoiTaoDMKho()
    '    'CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.KHO, cbbKhoHang.SelectedIndex).Ten

    '    'KhoiTaoDMLoHang(GetstrLoc())
    'End Sub

    'Public Sub New(ByVal ma_loaikho As String, ByVal maloaithuoc As String)
    '    'dung khoi tao dm lo hang cho phieu xuat
    '    ' This call is required by the Windows Form Designer.
    '    InitializeComponent()

    '    ' Add any initialization after the InitializeComponent() call.
    '    mDMLoaiKho = ma_loaikho
    '    ''''''''''''''''''''''''''''''''''''''''''''''''''''Ghi Chu
    '    ''''''''''''''''''''''''''''''''''''''''''''''''''''Sua
    '    'bo loc theo loai thuoc (yeu cau)
    '    'mMaHang = maloaithuoc
    '    'loc nhung lo hang theo loai kho, loai mat hang va co SLTon > 0
    '    mSSSLTon = ">"
    '    mSLTon = 0
    '    KhoiTaoDMKho()
    'End Sub

    Public Sub New(ByVal ma_loaikho As String, ByVal ma_kho As String, ByVal mathang_id As Integer)
        'dung khoi tao dm lo hang cho phieu xuat
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mDMLoaiKho = ma_loaikho
        'loc nhung lo hang theo loai kho, loai mat hang va co SLTon > 0
        'mSSSLTon = ">"
        'mSLTon = 0
        KhoiTaoDMKho()
        cbbKhoHang.SelectedValue = ma_kho
        KhoiTaoDMLoHang(GetstrLoc())
        lbxLoHang.Focus()
        lbxLoHang.SelectedValue = mathang_id
    End Sub


#Region "KhoiTao"
    Private Sub KhoiTaoDMKho()
        'GetDonVi_KhoListByStrDMKho
        'Dim strSql As String
        'strSql = "select L,KH,Ten,NH from tblDMKhac where KH = 'K'" '& strWhereDMKhoByUser
        'Dim aryKH As ArrayList = CDoiTuongs.GetDoiTuongListBySql(strSql)
        Dim aryKH As ArrayList = CDoiTuongs.GetDoiTuongList(enumDOITUONG.KHO)
        If aryKH.Count > 0 Then
            cbbKhoHang.DataSource = aryKH
            cbbKhoHang.ValueMember = "KH"
            cbbKhoHang.DisplayMember = "Ten"
        End If

        If (aryKH.Count <> 0) Then
            cbbKhoHang.SelectedIndex = 0
        End If
    End Sub

    Private Sub KhoiTaoDMLoHang(ByVal strwhere As String)
        Dim aryDTs As ArrayList = CMatHangs.GetMatHangList(strwhere)
        lbxLoHang.DataSource = aryDTs
        lblTongMatHang.Text = lbxLoHang.Items.Count.ToString + " Mặt Hàng"
        Try
            If (lbxLoHang.Items.Count <> 0) Then
                lbxLoHang.ValueMember = "mathang_id"
                lbxLoHang.DisplayMember = "ten"
                lbxLoHang.SelectedIndex = 0
            Else
                txtTenHang.Text = ""
                txtMa_Lo.Text = ""
                txtNgTTon.Text = ""
                txtCCL.Text = ""
                txtHSD.Text = ""
                txtDonGia.Text = ""
                txtTonKH.Text = ""
                txtTonTH.Text = ""
            End If
        Catch ex As Exception

        End Try

    End Sub

    Public Sub KhoiTaodtgPhieuNhapXuat(ByVal MATHANG_ID As String)
        'nen  tao view phieunhap va view phieu xuat
        'khoi tao dtgPhieuNhap
        Dim strLoc As String = GetstrLoc()
        Dim strSql As String
        'strSql = "select NX, (case when(bcth = 1)then 'KH' else 'TC' end) as TT ,sophieu, (CAST(day(NGAY) AS nvarchar(2)) + '/' + cast(month(NGAY)as nvarchar(2)) + '/'+ cast(YEAR(NGAY) as nvarchar(4))) as Ngay, tendvgn, tentcnx, dbo.ChuanHoaSo(slkh) as slkh, dbo.ChuanHoaSo(slth) as slth from viewNhapXuat " & _
        '                          "where MATHANG_ID = '" & MATHANG_ID & "'"
        strSql = "SELECT SLNX_ID, NGAY, SONHAP, SOXUAT, TEN_DVGN, SLKH , GHICHU_SOKHO FROM viewSOKHO " & _
                 GetstrLocdtg(strLoc) & " AND MATHANG_ID = '" & MATHANG_ID & "' order by NGAY"
        Dim ds As DataSet
        ds = BaseDB.ExecSql_DataSet(strSql)
        dtgNhapXuat.DataSource = ds.Tables(0)
        dtgNhapXuat.RetrieveStructure()
        'dtgNhapXuat.RootTable.Groups.Add(New Janus.Windows.GridEX.GridEXGroup(dtgNhapXuat.RootTable.Columns("NX")))
        'dtgNhapXuat.RootTable.SortKeys.Add(New Janus.Windows.GridEX.GridEXSortKey(dtgNhapXuat.RootTable.Columns("duoi_sophieu"), SortOrder.Ascending))
        'dtgNhapXuat.RootTable.SortKeys.Add(New Janus.Windows.GridEX.GridEXSortKey(dtgNhapXuat.RootTable.Columns("so_sophieu"), SortOrder.Ascending))
        dtgNhapXuat.GroupByBoxVisible = False
        FormatGridStyledtgNhapXuat()
        '-------------------------------------------------------------------
        'Dim cl As New DataColumn("stt")
        'ds.Tables(0).Columns.Add(cl)
        'ds.Tables(0).DefaultView.Sort() = "sophieu DESC"
        'Dim i As Integer = 0
        'For Each row As DataRow In ds.Tables(0).Rows
        '    row("stt") = i
        '    i += 1
        'Next

        'olgridNhapXuat.BindData(ds, "Table")
        'If ds.Tables(0).Rows.Count > 0 Then
        '    FormatGridStyleolgridNhapXuat()
        '    Dim ColumnIndex As Integer = 0 'group theo nhom numcol=0

        '    '// set the group template to use, e.g. to sort alphabetically:
        '    olgridNhapXuat.GroupTemplate = New OutlookgGridDefaultGroup 'OutlookGridAlphabeticGroup()

        '    '// specify the column the Group will be associated with:
        '    olgridNhapXuat.GroupTemplate.Column = olgridNhapXuat.Columns(ColumnIndex)

        '    '// all groups in the list will be collapsed,
        '    '// so only the groups are displayed, not the items
        '    olgridNhapXuat.GroupTemplate.Collapsed = False
        '    '// sort the grid using the DataRowComparer object
        '    '// the DataRowComparer constructor takes two parameters,
        '    '// the column that will be sorted on, and the direction
        '    '// in which to sort (ascending or descending)
        '    Try
        '        olgridNhapXuat.Sort(New DataRowComparer(0, ListSortDirection.Ascending))
        '        'olgridNhapXuat.Sort(olgridNhapXuat.Columns(ColumnIndex), ListSortDirection.Ascending)
        '    Catch ex As Exception
        '        MsgBox(ex.Source + ex.Message)
        '    End Try
        'End If
    End Sub

    Public Sub FormatGridStyledtgNhapXuat()
        With dtgNhapXuat.RootTable
            With .Columns.Item("SLNX_ID")
                .Visible = False
            End With
            With .Columns.Item("NGAY")
                .Width = 80
                .Caption = "NGÀY"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .EditType = Janus.Windows.GridEX.EditType.NoEdit
            End With
            With .Columns.Item("SONHAP")
                .Width = 60
                .Caption = "NHẬP SỐ"
                .CellStyle.ForeColor = Color.Blue
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .EditType = Janus.Windows.GridEX.EditType.NoEdit
            End With
            With .Columns.Item("SOXUAT")
                .Width = 60
                .Caption = "XUẤT SỐ"
                .CellStyle.ForeColor = Color.Blue
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .EditType = Janus.Windows.GridEX.EditType.NoEdit
            End With

            With .Columns.Item("TEN_DVGN")
                .Width = 140
                .Caption = "ĐƠN VỊ GIAO NHẬN"
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .EditType = Janus.Windows.GridEX.EditType.NoEdit
            End With
            With .Columns.Item("GHICHU_SOKHO")
                .Width = 140
                .Caption = "GHI CHÚ"
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .DefaultValue = ""
            End With
            With .Columns.Item("SLKH")
                .Width = 80
                .Caption = "Số lượng"
                .FormatString = "#,###.##"
                .TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
                .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
                .EditType = Janus.Windows.GridEX.EditType.NoEdit
            End With
            'With .Columns.Item("SL_XUAT")
            '    .Width = 80
            '    .Caption = "SL XUẤT"
            '    .FormatString = "#,###.##"
            '    .TextAlignment = Janus.Windows.GridEX.TextAlignment.Far
            '    .HeaderAlignment = Janus.Windows.GridEX.TextAlignment.Center
            '    .EditType = Janus.Windows.GridEX.EditType.NoEdit
            'End With
        End With
    End Sub
#End Region

#Region "ListBox"
    ''Code: Tuan
    'Private Sub lbxLoHang_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxLoHang.DoubleClick
    '    If fCallPhieuNX IsNot Nothing Then
    '        If Not IsNothing(Me.lbxLoHang.SelectedItem) Then
    '            If fCallPhieuNX.Name Is frmPhieuNX.Name Then
    '                Dim frm As frmPhieuNX = CType(fCallPhieuNX, frmPhieuNX)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            ElseIf fCallPhieuNX.Name Is frmPhieuNXNoiBo.Name Then
    '                Dim frm As frmPhieuNXNoiBo = CType(fCallPhieuNX, frmPhieuNXNoiBo)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            ElseIf fCallPhieuNX.Name Is frmPhieuNXDCSS.Name Then
    '                Dim frm As frmPhieuNXDCSS = CType(fCallPhieuNX, frmPhieuNXDCSS)
    '                Dim obj As CMatHang = Me.lbxLoHang.SelectedItem
    '                frm.AddMatHang(obj.MatHang_ID)
    '            End If
    '        End If
    '    End If
    'End Sub

    'Private Sub lbxLoHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lbxLoHang.KeyDown
    '    If e.KeyCode = Keys.Enter Then
    '        'lbxLoHang_DoubleClick(Nothing, Nothing)
    '    ElseIf e.KeyCode = Keys.Delete Then
    '        XoaLoHangCmnu_Click(Nothing, Nothing)
    '    End If
    'End Sub

    Private Sub lbxLoHang_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbxLoHang.DrawItem
        Try
            Dim brText As Brush
            e.DrawBackground()
            Dim objDT As CMatHang = Me.lbxLoHang.Items(e.Index)  'Me.lstItems.SelectedItem
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.White
            ElseIf objDT.SLTonKH = 0 Then
                brText = Brushes.Gray
            Else
                brText = Brushes.Blue
            End If
            'If (flagHSD) Then
            '    Dim hsd As String
            '    If (objDT.HanSD <> "") Then
            '        hsd = objDT.HanSD
            '    Else
            '        hsd = "H12/49"
            '    End If
            '    If (Not SoSanhHSD(hsd, mtbHSD.Text)) Then
            '        brText = Brushes.Red
            '    End If
            'End If
            If objDT.Lo <> "" Then
                e.Graphics.DrawString(objDT.Ten + " - " + objDT.Lo, Me.lbxLoHang.Font, brText, e.Bounds.X + 5, e.Bounds.Y)
            Else
                e.Graphics.DrawString(objDT.Ten, Me.lbxLoHang.Font, brText, e.Bounds.X + 5, e.Bounds.Y)
            End If

        Catch ex As Exception
        Finally
            'If Not brColor Is Nothing Then
            'brColor.Dispose()
            'End If
        End Try
    End Sub

    Private Sub lbxLoHang_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxLoHang.SelectedIndexChanged
        Try
            If lbxLoHang.Items.Count <> 0 Then
                Dim obj_MatHang As CMatHang = lbxLoHang.SelectedItem
                txtTenHang.Text = obj_MatHang.Ten
                txtMa_Lo.Text = obj_MatHang.Ma + "." + obj_MatHang.Code + "   " + obj_MatHang.Lo
                txtNgTTon.Text = obj_MatHang.NgTTon.ToString(FormatDateStyle())
                txtCCL.Text = obj_MatHang.H
                txtHSD.Text = obj_MatHang.HanSD
                txtDonGia.Text = obj_MatHang.Gle.ToString(FormatMoneyStyle())
                txtLoaiKho.Text = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, obj_MatHang.LK).Ten
                txtTonKH.Text = obj_MatHang.SLTonKH.ToString(FormatQuantityStyle())
                txtTonTH.Text = obj_MatHang.SLTonTH.ToString(FormatQuantityStyle())
                txtKiemKeDauNam.Text = obj_MatHang.SLKK11.ToString(FormatQuantityStyle())
                txtLuyTichNhapKH.Text = obj_MatHang.SLNDKKH.ToString(FormatQuantityStyle())
                txtLuyTichXuatKH.Text = obj_MatHang.SLXDKKH.ToString(FormatQuantityStyle())
                txtLuyTichNhapTH.Text = obj_MatHang.SLNDKTH.ToString(FormatQuantityStyle())
                txtLuyTichXuatTH.Text = obj_MatHang.SLXDKTH.ToString(FormatQuantityStyle())
                txtSLSS.Text = obj_MatHang.SLTonSS.ToString(FormatQuantityStyle())
                txtSLSSDauNam.Text = obj_MatHang.SLSS11.ToString(FormatQuantityStyle())
                txtDVT.Text = obj_MatHang.DVT
                KhoiTaodtgPhieuNhapXuat(obj_MatHang.MatHang_ID)
            End If
        Catch ex As Exception
            ShowMessages(ex.ToString())
        End Try
    End Sub

#End Region

#Region "Event, btnLoc"
    Private Sub btnLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If (cbbKhoHang.SelectedIndex <> -1) Then
            Dim frm As New frmLocLoHang(Me, CType(cbbKhoHang.SelectedItem, CDoiTuong).KH)
            frm.ShowDialog()
            KhoiTaoDMLoHang(GetstrLoc())
        End If
    End Sub

    Private Function GetstrLoc() As String
        Dim strsql As String = ""

        ''loai kho
        'If (mDMLoaiKho <> "") Then
        '    Dim str() As String
        '    str = mDMLoaiKho.Split("/")
        '    strsql &= "("
        '    For Each s As String In str
        '        If (strsql.Length > 1) Then ' vi them ( vao roi
        '            strsql &= " or (LK = '" & s & "')"
        '        Else
        '            strsql &= " (LK = '" & s & "')"
        '        End If
        '    Next
        '    strsql &= ")"
        'End If

        ''ten hang
        'If (mTenHang <> "") Then
        '    If (strsql.Length > 0) Then
        '        strsql &= " and ten like N'" & mTenHang & "%'"
        '    Else
        '        strsql &= " ten like N'" & mTenHang & "%'"
        '    End If
        'End If

        ''ma hang
        'If (mMaHang <> "") Then
        '    If (mMaHang.Substring(7, 7) = "0000000") Then 'loai thuoc
        '        'xuli chuoi: loai bo nhung ki tu 0 dung sau de lay duoc loai thuoc
        '        Dim loaithuoc As String = mMaHang.Substring(0, 7)
        '        For i As Integer = 0 To 6
        '            If (loaithuoc.Chars(6 - i) = "0") Then
        '                loaithuoc = loaithuoc.Remove(6 - i, 1)
        '            Else
        '                Exit For
        '            End If
        '        Next
        '        'lay left
        '        If (strsql.Length > 0) Then
        '            strsql &= " and left(ma," + CStr(loaithuoc.Length) + ") = '" + loaithuoc + "'"
        '        Else
        '            strsql &= " left(ma," + CStr(loaithuoc.Length) + ") = '" + loaithuoc + "'"
        '        End If
        '    Else 'thuoc
        '        If (strsql.Length > 0) Then
        '            strsql &= " and ma = '" + mMaHang + "'"
        '        Else
        '            strsql &= " ma = '" + mMaHang + "'"
        '        End If
        '    End If

        'End If
        ''lohang
        'If (mLoHang <> "") Then
        '    If (strsql.Length > 0) Then
        '        strsql &= " and lo = '" + mLoHang + "'"
        '    Else
        '        strsql &= " lo = '" + mLoHang + "'"
        '    End If
        'End If
        ''cap chat luong
        'If (mCCL <> "") Then
        '    If (strsql.Length > 0) Then
        '        strsql &= " and h = '" + mCCL + "'"
        '    Else
        '        strsql &= " h = '" + mCCL + "'"
        '    End If
        'End If
        ''han su dung

        'If (mSSHSD <> "") Then
        '    'chuyen doi ngay trong sql

        '    If (strsql.Length > 0) Then
        '        strsql &= " and convert(datetime,hansd_hoanchinh)" + mSSHSD + "'" & Format(mHSD, "M/d/yyyy") & "'"
        '    Else
        '        strsql &= " convert(datetime,hansd_hoanchinh)" + mSSHSD + "'" & Format(mHSD, "M/d/yyyy") & "'"
        '    End If
        'End If

        'If (mSSSLTon <> "") And (mSLTon <> "") Then
        '    If (strsql.Length > 0) Then
        '        strsql &= " and SLTonkh" + mSSSLTon + "'" + mSLTon + "'"
        '    Else
        '        strsql &= " SLTonkh" + mSSSLTon + "'" + mSLTon + "'"
        '    End If
        'End If




        'Update 2014-05-22:
        'HT Code: Sửa tcsl = 1 thành (bcth = 4 or bcth = 2)
        strsql = "WHERE mathang_id in (select mathang_id from viewnhapxuat where (bcth = 4 or bcth =2 )"


        'loc theo ngay
        If rbtNam.Checked Then
            strsql += " AND NGAY >= '01/01/" & Now.Year.ToString & "' AND NGAY <= '12/31/" & Now.Year.ToString & "'"
        End If

        If rbtQuiI.Checked Then
            strsql += " AND NGAY >= '01/01/" & Now.Year.ToString & "' AND NGAY <= '03/31/" & Now.Year.ToString & "'"
        End If

        If rbtQuiII.Checked Then
            strsql += " AND NGAY >= '04/01/" & Now.Year.ToString & "' AND NGAY <= '06/30/" & Now.Year.ToString & "'"
        End If

        If rbtQuiIII.Checked Then
            strsql += " AND NGAY >= '07/01/" & Now.Year.ToString & "' AND NGAY <= '09/30/" & Now.Year.ToString & "'"
        End If

        If rbtQuiIV.Checked Then
            strsql += " AND NGAY >= '10/01/" & Now.Year.ToString & "' AND NGAY <= '12/31/" & Now.Year.ToString & "'"
        End If

        If rbtNgay.Checked Then
            strsql += " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
        End If

        'Loc nhet vao giua

        'Nếu thuộc tính chọn là <Chọn tất cả> (mã = 0001)
        If CType(cbbKhoHang.SelectedItem, CDoiTuong).KH <> "0001" Then
            strsql += " and kho = '" + CType(cbbKhoHang.SelectedItem, CDoiTuong).KH + "'"

        End If


        strsql += ")"
        Return strsql
    End Function

    Private Sub cbbKhoHang_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbKhoHang.SelectedIndexChanged
        If (cbbKhoHang.SelectedIndex <> -1) Then
            KhoiTaoDMLoHang(GetstrLoc())
            'InitListView(GetstrLoc())
        End If
    End Sub

    'Private Sub mtbHSD_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If (e.KeyCode = Keys.Enter) Then
    '        If (KiemTraNhap_mtbHSD()) Then
    '            flagHSD = True
    '            lbxLoHang.Refresh()
    '            mtbHSD.ReadOnly = True
    '        End If
    '    End If
    '    If (e.KeyCode = Keys.Delete) And (e.Modifiers = Keys.Control) Then
    '        flagHSD = False
    '        lbxLoHang.Refresh()
    '        mtbHSD.ReadOnly = False
    '        mtbHSD.Text = ""
    '    End If
    'End Sub

    'Public Function KiemTraNhap_mtbHSD() As Boolean
    '    Try
    '        Dim kq As Boolean = True
    '        Dim hsd As String
    '        hsd = mtbHSD.Text.Substring(1)
    '        Dim str() As String
    '        str = hsd.Split("/")
    '        If (str(0).Trim <> "") Then
    '            If (CInt(str(0)) > 12) Or (CInt(str(0)) < 1) Then
    '                kq = False
    '                MsgBox("Bạn nhập tháng HSD cần tìm chưa đúng")
    '            End If
    '        Else
    '            kq = False
    '            MsgBox("Bạn chưa nhập tháng HSD cần tìm ")
    '        End If
    '        If (str(1).Trim.Length <> 2) Then
    '            MsgBox("Bạn nhập năm HSD cần tìm chưa đúng")
    '            kq = False
    '        End If
    '        Return kq
    '    Catch ex As Exception
    '        MsgBox("Bạn nhập HSD cần tìm chưa đúng")
    '    End Try
    'End Function

    Public Function SoSanhHSD(ByVal HSDa As String, ByVal HSDb As String) As Boolean
        ' true : a>b else false
        Dim kq As Boolean = False
        Dim thanga, nama, thangb, namb As Integer
        thanga = CInt(HSDa.Substring(1, 2))
        nama = CInt(HSDa.Substring(4, 2))
        thangb = CInt(HSDb.Substring(1, 2))
        namb = CInt(HSDb.Substring(4, 2))
        If (nama > namb) Then
            kq = True
        ElseIf (nama = namb) Then
            If (thanga > thangb) Then
                kq = True
            End If
        End If
        Return kq
    End Function


#End Region

#Region "btn BoLoc, ThongKe"
    Private Sub btnThongKeNhapXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThongKeNhapXuat.Click
        'bieu thong ke nhap xuat
        '
        'Dim strsql As String
        'strsql = "select * from viewNhapXuat where mathang_id = '" & CStr(CType(lbxLoHang.SelectedItem, CMatHang).MatHang_ID) & "' order by NX"
        'Dim ds As DataSet
        'ds = BaseDB.ExecSql_DataSet(strsql)
        ''
        'If (MsgBox("Chọn Yes để In, No để Xuất ra Excel", MsgBoxStyle.YesNo, "Thống kê Nhập Xuất") = MsgBoxResult.Yes) Then
        '    'in
        '    Dim nnreport As New rptBaoCaoThongKeNhapXuatTheoLoHang
        '    nnreport.SetDataSource(ds.Tables(0))
        '    nnreport.SetParameterValue(0, txtLoaiKho.Text)
        '    strsql = "select count(*) from tblDPKK where Year(ngaykk)= '" & CStr(Now.Year) & "'"
        '    Dim value As Integer = 0
        '    value = BaseDB.ExecSql_DataValue(strsql)
        '    nnreport.SetParameterValue(1, value)
        '    Dim frm As New frmReportViewer(nnreport)
        '    frm.ShowDialog()
        'Else
        '    'xuat ra excel
        '    Dim mFileName2 As String = Application.StartupPath & "\TempTHONGKENHAPXUAT.xls"
        '    ToEXCEL(mFileName2, ds)
        'End If

        Dim strLoc As String = GetstrLoc()
        Dim strSql As String

        strSql = "SELECT * FROM VIEWSOKHO " + GetstrLocdtg(strLoc) + " ORDER BY TEN, NGAY"
        Dim ds As DataSet
        ds = BaseDB.ExecSql_DataSet(strSql)
        Dim nnreport As New rptSoKho
        nnreport.SetDataSource(ds.Tables(0))
        nnreport.SetParameterValue(0, cbbKhoHang.Text.ToUpper)
        Dim frm As New frmReportViewer(nnreport)
        frm.ShowDialog()
    End Sub

    Private Sub btnBoLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim strWhere As String
        strWhere = "where kho = '" + CType(cbbKhoHang.SelectedItem, CDoiTuong).KH + "'"
        KhoiTaoDMLoHang(strWhere)

        'set lai gia tri loc da luu
        mMaHang = ""
        mLoHang = ""
        mCCL = ""
        mSSHSD = ""
        mHSD = Now.Date
        mSSSLTon = ""
        mSLTon = ""
        mDMLoaiKho = ""
    End Sub
#End Region

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

#Region "Excel"
    Private Sub ToEXCEL(ByVal mFileName2 As String, ByVal m_ds As DataSet)
        Dim mFileName As String = Application.StartupPath & "\THONGKENHAPXUAT.xls"

        System.IO.File.Copy(mFileName, mFileName2, True)
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        Dim mRange As Excel.Range

        xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
        xlBook = CType(xlApp.Workbooks.Open(mFileName2), Excel.Workbook)

        xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)

        xlSheet.Activate()
        xlSheet.Application.Visible = True

        'dau phieu
        Dim objMatHang As CMatHang = CType(lbxLoHang.SelectedItem, CMatHang)
        xlSheet.Cells(2, 3) = "MẶT HÀNG : " + objMatHang.Ma + "-" + objMatHang.Code
        xlSheet.Cells(3, 3) = objMatHang.Ten
        mRange = xlSheet.Cells(3, 3)
        With mRange.Font
            .Size = 16
        End With
        xlSheet.Cells(4, 2) = "Đơn vị tính : " + objMatHang.DVT
        mRange = xlSheet.Cells(3, 4)
        With mRange.Font
            .Size = 16
            .Bold = True
        End With
        xlSheet.Cells(5, 2) = "Lô : " + objMatHang.Lo
        xlSheet.Cells(6, 2) = "Loại kho : " + CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, objMatHang.LK).Ten

        xlSheet.Cells(4, 4) = "Chất lượng : " + objMatHang.H
        xlSheet.Cells(5, 4) = "Hạn dùng : " + objMatHang.HanSD + "  Giá lẻ : " + CStr(objMatHang.Gle)
        xlSheet.Cells(6, 5) = "Kho : " + CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.KHO, objMatHang.Kho).Ten

        'SLKH.......
        xlSheet.Cells(8, 4) = objMatHang.SLKK11
        xlSheet.Cells(9, 4) = objMatHang.SLNDKKH
        xlSheet.Cells(10, 4) = objMatHang.SLXDKKH
        xlSheet.Cells(11, 4) = objMatHang.SLTonKH
        xlSheet.Cells(12, 4) = objMatHang.SLTonTH
        'Thanh tien ......
        Dim gle As Double = objMatHang.Gle
        xlSheet.Cells(8, 6) = objMatHang.SLKK11 * gle
        xlSheet.Cells(9, 6) = objMatHang.SLNDKKH * gle
        xlSheet.Cells(10, 6) = objMatHang.SLXDKKH * gle
        xlSheet.Cells(11, 6) = objMatHang.SLTonKH * gle
        xlSheet.Cells(12, 6) = objMatHang.SLTonTH * gle


        Dim iRow0 As Integer = 15
        Dim len, i As Integer
        Dim ngay_vietphieu, nx, so_phieu, dvgn, ngay_nhan As String
        Dim slth, slkh As Double
        Dim nhap, xuat, so_donvinhap, so_donvixuat As Integer
        Dim tempdvgn_nhap, tempdvgn_xuat As String
        tempdvgn_nhap = ""
        tempdvgn_xuat = ""
        'Dim flag As Boolean = False

        i = 0
        '(i chay trong ds, j chay trong excel)
        len = m_ds.Tables(0).Rows.Count
        While (i < len)
            If (i >= len) Then
                Exit While
            End If
            ngay_vietphieu = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("NGAY"))
            nx = m_ds.Tables(0).Rows.Item(i).Item("NX")
            so_phieu = m_ds.Tables(0).Rows.Item(i).Item("SOPHIEU")
            dvgn = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("DVGN"))
            ngay_nhan = ChuanHoaChuoi(m_ds.Tables(0).Rows.Item(i).Item("NGAYGN"))
            slth = m_ds.Tables(0).Rows.Item(i).Item("SLTH")
            slkh = m_ds.Tables(0).Rows.Item(i).Item("SLKH")

            If (nx = "N") Then
                nhap += 1
                If (dvgn <> tempdvgn_nhap) Then
                    so_donvinhap += 1
                    tempdvgn_nhap = dvgn
                End If
            Else
                xuat += 1
                If (dvgn <> tempdvgn_xuat) Then
                    so_donvixuat += 1
                    tempdvgn_xuat = dvgn
                End If
            End If

            xlSheet.Cells(iRow0 + i, 1) = GetDate(ngay_vietphieu)
            xlSheet.Cells(iRow0 + i, 2) = nx + "  " + so_phieu
            xlSheet.Cells(iRow0 + i, 3) = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.DONVI, dvgn).Ten
            xlSheet.Cells(iRow0 + i, 4) = GetDate(ngay_nhan)
            xlSheet.Cells(iRow0 + i, 5) = slkh
            xlSheet.Cells(iRow0 + i, 6) = slkh
            '
            i += 1
            If (i >= len) Then
                Exit While
            End If
        End While
        'in tong cong
        If (i <> 0) Then
            Dim iRowsCount = i - 1
            Dim mColsCount = 6
            'col1 AlignCenter

            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, 1))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            End With
            '
            'col4 AlignCenter
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 4), xlSheet.Cells(iRow0 + iRowsCount, 4))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            End With
            '
            'col5,6 AlignRight
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 5), xlSheet.Cells(iRow0 + iRowsCount, 6))
            With mRange
                .HorizontalAlignment = Excel.XlHAlign.xlHAlignRight
            End With
            '
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, mColsCount))
            With mRange
                .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
            End With
            mRange = xlSheet.Range(xlSheet.Cells(iRow0 - 1, 1), xlSheet.Cells(iRow0 + iRowsCount, mColsCount))
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeLeft)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeTop)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeBottom)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlEdgeRight)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
            With mRange.Borders(Excel.XlBordersIndex.xlInsideVertical)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With

            With mRange.Borders(Excel.XlBordersIndex.xlInsideHorizontal)
                .LineStyle = Excel.XlLineStyle.xlContinuous
                .Weight = Excel.XlBorderWeight.xlThin
            End With
        End If
        xlApp.ActiveWindow.Visible = False
        xlApp.Windows(1).Activate()
        '
        'duoi phieu
        xlSheet.Cells(iRow0 + i + 1, 2) = "Số lượng phiếu Nhập : " + CStr(nhap)
        xlSheet.Cells(iRow0 + i + 2, 2) = "Số lượng phiếu Xuất : " + CStr(xuat)
        xlSheet.Cells(iRow0 + i + 1, 4) = "Nhập hàng từ: " + CStr(so_donvinhap)
        xlSheet.Cells(iRow0 + i + 2, 4) = "Xuất hàng cho : " + CStr(so_donvixuat)
        xlSheet.Cells(iRow0 + i + 4, 5) = "In Ngày " + CStr(Now.Day) + "/" + CStr(Now.Month) + "/" + CStr(Now.Year)
    End Sub

    Function GetDate(ByVal ngay As Object) As String
        If (Not ngay Is DBNull.Value) Then
            Try
                Dim n As Date = CDate(ngay)
                Return CStr(n.Day) + "/" + CStr(n.Month) + "/" + CStr(n.Year)
            Catch ex As Exception
                'neu ko ep duoc kieu ngay thi tra ve du lieu hien co 
                Return CStr(ngay)
            End Try
        End If
        Return ""
    End Function
#End Region

    'Private Sub XoaLoHangCmnu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaLoHangCmnu.Click
    '    If (dtgNhapXuat.RowCount <> 0) Then
    '        ShowMessages("Bạn không được xóa lô hàng này vì liên quan đến phiếu Nhập Xuất")
    '        Exit Sub
    '    End If
    '    Try
    '        Dim obj_MatHang As CMatHang = lbxLoHang.SelectedItem
    '        If (obj_MatHang.SLKK11 = 0) And _
    '           (obj_MatHang.SLNDKKH = 0) And _
    '           (obj_MatHang.SLNDKTH = 0) And _
    '           (obj_MatHang.SLXDKKH = 0) And _
    '           (obj_MatHang.SLXDKTH = 0) And _
    '           (obj_MatHang.SLTonKH = 0) And _
    '           (obj_MatHang.SLTonTH = 0) And _
    '           (dtgNhapXuat.RowCount = 0) Then
    '            Dim index = lbxLoHang.SelectedIndex
    '            CMatHangs.DeleteMatHang(obj_MatHang)
    '            KhoiTaoDMLoHang(GetstrLoc())
    '            lbxLoHang.SelectedIndex = index - 1
    '            lbxLoHang.Focus()
    '            Exit Sub
    '        Else
    '            ShowMessages("Liên quan đến phiếu Nhập, Xuất. Không thể xóa được.")
    '            Exit Sub
    '        End If
    '    Catch ex As Exception
    '        MsgBox(ex.Source)
    '    End Try
    '    'chua xet so phieu nhap xuat co trong mat hang nay.
    '    'Dim strsql As String
    '    'strsql = "select count(*) from from viewNhapXuat " & _
    '    '          "where MATHANG_ID = '" & MATHANG_ID & "'"
    '    ShowMessages("Số liệu này khác 0 nên bạn không được xóa")
    'End Sub

#Region "ListView"
    'Private Sub frmQuanLyLoHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '    With lvDanhMuc
    '        .HideSelection = False ' = True de ton tai truong hop e.state <> ListViewItemStates.Selected
    '        .OwnerDraw = False
    '        .FullRowSelect = True
    '        .GridLines = True
    '        .View = View.Details
    '        .Columns.Add("Tên", 80, HorizontalAlignment.Left)
    '        .Columns.Add("Lô hàng", 80, HorizontalAlignment.Left)
    '        .Columns.Add("Số lượng", lvDanhMuc.Width - lvDanhMuc.Columns(0).Width - lvDanhMuc.Columns(1).Width - 20, HorizontalAlignment.Left)
    '    End With
    'End Sub


    'Private Sub lvDanhMuc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvDanhMuc.SelectedIndexChanged
    '    Try
    '        If lvDanhMuc.SelectedItems.Count > 0 Then
    '            Dim obj_MatHang As CMatHang = CMatHangs.GetMatHangByMa(CInt(lvDanhMuc.SelectedItems(0).Name))
    '            txtTenHang.Text = obj_MatHang.Ten
    '            txtMa_Lo.Text = obj_MatHang.Ma + "." + obj_MatHang.Code + "   " + obj_MatHang.Lo
    '            txtNgTTon.Text = obj_MatHang.NgTTon.ToString(FormatDateStyle())
    '            txtCCL.Text = obj_MatHang.H
    '            txtHSD.Text = obj_MatHang.HanSD
    '            txtDonGia.Text = obj_MatHang.Gle.ToString(FormatMoneyStyle())
    '            txtLoaiKho.Text = CDoiTuongs.GetDoiTuongByMa(enumDOITUONG.LOAIKHO, obj_MatHang.LK).Ten
    '            txtTonKH.Text = obj_MatHang.SLTonKH.ToString(FormatQuantityStyle())
    '            txtTonTH.Text = obj_MatHang.SLTonTH.ToString(FormatQuantityStyle())
    '            txtKiemKeDauNam.Text = obj_MatHang.SLKK11.ToString(FormatQuantityStyle())
    '            txtLuyTichNhapKH.Text = obj_MatHang.SLNDKKH.ToString(FormatQuantityStyle())
    '            txtLuyTichXuatKH.Text = obj_MatHang.SLXDKKH.ToString(FormatQuantityStyle())
    '            txtLuyTichNhapTH.Text = obj_MatHang.SLNDKTH.ToString(FormatQuantityStyle())
    '            txtLuyTichXuatTH.Text = obj_MatHang.SLXDKTH.ToString(FormatQuantityStyle())
    '            txtSLSS.Text = obj_MatHang.SLTonSS.ToString(FormatQuantityStyle())
    '            txtSLSSDauNam.Text = obj_MatHang.SLSS11.ToString(FormatQuantityStyle())
    '            txtDVT.Text = obj_MatHang.DVT
    '            KhoiTaodtgPhieuNhapXuat(obj_MatHang.MatHang_ID)
    '        End If
    '    Catch ex As Exception
    '        ShowMessages(ex.ToString())
    '    End Try
    'End Sub

    'Public Sub InitListView(ByVal strwhere As String)
    '    Dim aryDTs As ArrayList = CMatHangs.GetMatHangList(strwhere)
    '    lvDanhMuc.Items.Clear()
    '    lblTongMatHang.Text = aryDTs.Count.ToString + " Mặt Hàng"
    '    If aryDTs.Count > 0 Then
    '        Dim item As ListViewItem
    '        Dim subItem As ListViewItem.ListViewSubItem
    '        For Each obj As CMatHang In aryDTs
    '            item = New ListViewItem(obj.Ten)
    '            subItem = New ListViewItem.ListViewSubItem()
    '            'subItem.Name = .column_mahieu
    '            subItem.Text = obj.Lo
    '            item.SubItems.Add(subItem)

    '            subItem = New ListViewItem.ListViewSubItem()
    '            'subItem.Name = CBanDoCollection.column_ten_bando
    '            subItem.Text = obj.SLTonKH
    '            item.SubItems.Add(subItem)

    '            'add key of item
    '            item.Name = obj.MatHang_ID
    '            lvDanhMuc.Items.Add(item)
    '        Next
    '        lvDanhMuc.Focus()
    '        lvDanhMuc.Items(0).Selected = True
    '    Else
    '        txtTenHang.Text = ""
    '        txtMa_Lo.Text = ""
    '        txtNgTTon.Text = ""
    '        txtCCL.Text = ""
    '        txtHSD.Text = ""
    '        txtDonGia.Text = ""
    '        txtTonKH.Text = ""
    '        txtTonTH.Text = ""
    '    End If
    'End Sub

#Region "Draw Item"

    'Private Sub lvDanhMuc_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) Handles lvDanhMuc.DrawColumnHeader
    '    Dim sf As New StringFormat()
    '    Try
    '        sf.Alignment = StringAlignment.Center
    '        ' Draw the standard header background.
    '        e.DrawBackground()
    '        ' Draw the header text.
    '        'Dim headerFont As New Font("Courier New", 9, FontStyle.Bold)
    '        e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.Black, e.Bounds, sf)
    '    Catch ex As Exception
    '    Finally
    '        sf.Dispose()
    '    End Try
    'End Sub

    '' Draws the backgrounds for entire ListView items.
    'Private Sub lvDanhMuc_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) Handles lvDanhMuc.DrawItem
    '    If Not (e.State And ListViewItemStates.Selected) = 0 Then
    '        'Dim objDT As CMatHang = Me.lvDanhMuc.Items(e.Index)  'Me.lstItems.SelectedItem
    '        'If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    '        '    brText = Brushes.White
    '        'ElseIf objDT.SLTonKH = 0 Then
    '        '    brText = Brushes.Gray
    '        'Else
    '        '    brText = Brushes.Blue
    '        'End If
    '        ' Draw the background for a selected item.
    '        e.Graphics.FillRectangle(Brushes.DarkBlue, e.Bounds)
    '        e.DrawFocusRectangle()
    '    End If
    'End Sub

    'Private Sub lvDanhMuc_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) Handles lvDanhMuc.DrawSubItem
    '    Dim flags As TextFormatFlags = TextFormatFlags.Left
    '    Dim sf As New StringFormat()
    '    Try
    '        '' Store the column text alignment, letting it default
    '        '' to Left if it has not been set to Center or Right.
    '        Select Case e.Header.TextAlign
    '            Case HorizontalAlignment.Left
    '                sf.Alignment = StringAlignment.Near
    '                flags = TextFormatFlags.Left
    '            Case HorizontalAlignment.Center
    '                sf.Alignment = StringAlignment.Center
    '                flags = TextFormatFlags.HorizontalCenter
    '            Case HorizontalAlignment.Right
    '                sf.Alignment = StringAlignment.Far
    '                flags = TextFormatFlags.Right
    '        End Select

    '        ' Draw normal text for a subitem with a nonnegative 
    '        ' or nonnumerical value.
    '        If Not (e.ItemState And ListViewItemStates.Selected) = 0 Then
    '            e.Graphics.DrawString(e.SubItem.Text, Me.lvDanhMuc.Font, Brushes.White, e.Bounds, sf)
    '        Else
    '            e.DrawText()
    '        End If
    '    Finally
    '        sf.Dispose()
    '    End Try

    'End Sub
#End Region

#End Region


    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        'cap nhat ghi chu so kho
        For Each row As Janus.Windows.GridEX.GridEXRow In dtgNhapXuat.GetRows
            Dim SLNX_ID As Integer = row.Cells("SLNX_ID").Value
            Dim Ghichu As String = IIf(row.Cells("GHICHU_SOKHO").Value Is DBNull.Value, "", row.Cells("GHICHU_SOKHO").Value)
            Dim strUpd As String
            strUpd = "UPDATE tblSLNX " & _
                     " SET [GHICHU_SOKHO] = N'" & Ghichu & "'" & _
                     " WHERE SLNX_ID = " & SLNX_ID
            Try
                BaseDB.ExecSql_NoneQuery(strUpd)
            Catch ex As Exception
                MsgBox("Upd Ghi Chu So Kho: " + ex.Message)
            End Try
        Next
        MsgBox("Dữ liệu đã được cập nhật")
    End Sub


    Private Sub frmQuanLySoKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtNam.Checked = True
        dtpTuNgay.Visible = False
        dtpDenNgay.Visible = False
        lblTuNgay.Visible = False
        lblDenNgay.Visible = False
    End Sub

#Region "CheckBox Loc Bao Cao Theo Ngay"
    Private Sub rbtNam_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtNam.CheckedChanged
        If rbtNam.Checked Then
            KhoiTaoDMLoHang(GetstrLoc())
        End If
    End Sub

    Private Sub rbtNgay_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtNgay.CheckedChanged
        If rbtNgay.Checked Then
            dtpTuNgay.Value = "01/01/" + Now.Year.ToString
            dtpDenNgay.Value = "31/12/" + Now.Year.ToString
            dtpTuNgay.Visible = True
            dtpDenNgay.Visible = True
            lblTuNgay.Visible = True
            lblDenNgay.Visible = True
        Else
            dtpTuNgay.Visible = False
            dtpDenNgay.Visible = False
            lblTuNgay.Visible = False
            lblDenNgay.Visible = False
        End If
    End Sub

    Private Sub rbtQuiI_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtQuiI.CheckedChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub

    Private Sub rbtQuiII_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtQuiII.CheckedChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub

    Private Sub rbtQuiIII_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtQuiIII.CheckedChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub

    Private Sub rbtQuiIV_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtQuiIV.CheckedChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub

    Private Sub dtpTuNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpTuNgay.ValueChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub

    Private Sub dtpDenNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDenNgay.ValueChanged
        KhoiTaoDMLoHang(GetstrLoc())
    End Sub
#End Region


    Function GetstrLocdtg(ByVal strLoc As String) As String
        Dim kq As String
        kq = strLoc.Substring(57, strLoc.Length - 58)
        Return kq
    End Function

    
End Class